<?php
/**
 * LilBlogs plugin
 * This is a plugin which handles common posts and comments scenario
 *
 * @copyright     Copyright (c) 2010, Miha Nahtigal
 * @link          http://www.nahtigal.com/
 * @package       lil_blogs_plugin
 * @subpackage    lil_blogs_plugin.controllers
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
App::import('Controller', 'Lil.LilPlugin');
/**
 * LilBlogsPluginController class
 *
 * @uses          LilBlogsPluginController
 * @package       lil_blogs_plugin
 * @subpackage    lil_blogs_plugin.controllers
 */
class LilBlogsPluginController extends LilPluginController {
	var $name = 'LilBlogsPlugin';
/**
 * handlers property
 *
 * @var array
 * @access public
 */
	var $handlers = array(
		'admin_sidebar'   => array('function' => '_setAdminSidebar', 'params' => array()),
	);
/**
 * _setAdminSidebar method
 *
 * Add admin sidebar elements.
 *
 * @param mixed $model
 * @access public
 * @return void
 */
	function _setAdminSidebar($controller, $sidebar) {
		$blogs['visible'] = Configure::read('LilBlogs.allowEmptyBlog') || $this->currentArea->get('id');
		
		$blogs['items'] = array(
			'posts' => array(
				'visible' => true,
				'title' => __d('lil_blogs', 'Posts', true),
				'url'   => array(
					'admin'      => true,
					'plugin'     => 'lil_blogs',
					'controller' => 'posts',
					'action'     => 'index',
				),
				'params' => array(),
				'active' => in_array($this->params['controller'], array('posts', 'categories')) &&
					empty($this->params['named']['blog']),
				'expand' => in_array($this->params['controller'], array('posts', 'categories')) &&
					empty($this->params['named']['blog']),
				'submenu' => array(
					'edit' => array(
						'visible' => true,
						'title' => __d('lil_blogs', 'Edit', true),
						'url'   => array(
							'admin'      => true,
							'plugin'     => 'lil_blogs',
							'controller' => 'posts',
							'action'     => 'index',
						),
						'params' => array(),
						'active' =>
							$this->params['controller'] == 'posts' &&
							in_array($this->params['action'], array('edit', 'index'))
					),
					'add' => array(
						'visible' => true,
						'title' => __d('lil_blogs', 'Add New', true),
						'url'   => array(
							'admin'      => true,
							'plugin'     => 'lil_blogs',
							'controller' => 'posts',
							'action'     => 'add',
						),
						'params' => array(),
						'active' =>
							($this->params['controller'] == 'posts') &&
							($this->params['action'] == 'add')
					),
					'categories' => (Configure::read('LilBlogs.noCategories')) ? null : array(
						'visible' => true,
						'title' => __d('lil_blogs', 'Categories', true),
						'url'   => array(
							'admin'      => true,
							'plugin'     => 'lil_blogs',
							'controller' => 'categories',
							'action'     => 'index',
						),
						'params' => array(),
						'active' => $this->params['controller']=='categories'
					),
				)
			),
			'comments' => (Configure::read('LilBlogs.noComments')) ? null : array(
				'visible' => true,
				'title' => __d('lil_blogs', 'Comments', true),
				'url'   => array(
					'admin'      => true,
					'plugin'     => 'lil_blogs',
					'controller' => 'comments',
					'action'     => 'index',
				),
				'params' => array(),
				'active' => in_array($this->params['controller'], array('comments')),
				'expand' => $this->params['controller'] == 'comments',
				'submenu' => array(
					'all' => array(
						'visible' => true,
						'title' => !isset($this->Comment) ? __d('lil_blogs', 'All', true) :
						sprintf(__d('lil_blogs', 'All (%d)', true), $this->Comment->find('count', array(
							'conditions' => array_merge(
								empty($this->params['named']['post_id']) ? array() : array('Comment.post_id' => $this->params['named']['post_id']),
								array('Post.blog_id' => $blog['Blog']['id'])
							),
							'contain'    => array('Post'),
						))),
						'url'   => array_merge(
							array(
								'admin'      => true,
								'plugin'     => 'lil_blogs',
								'controller' => 'comments',
								'action'     => 'index',
							),
							empty($this->params['named']['post_id']) ? array() : array('post_id' => $this->params['named']['post_id'])
						),
						'params' => array(),
						'active' => (!isset($this->params['named']['status']))
					),
					'pending' => array(
						'visible' => true,
						'title' => !isset($this->Comment) ? __d('lil_blogs', 'Pending', true) :
						sprintf(__d('lil_blogs', 'Pending (%d)', true), $this->Comment->find('count', array(
							'conditions' => array_merge(
								empty($this->params['named']['post_id']) ? array() : array('Comment.post_id' => $this->params['named']['post_id']),
								array(
									'Post.blog_id' => $blog['Blog']['id'],
									'Comment.status' => LILCOMMENT_PENDING
								)
							),
							'contain'    => array('Post'),
						))),
						'url'   => array_merge(
							array(
								'plugin' => 'lil_blogs',
								'admin' => true,
								'controller' => 'comments',
								'action' => 'index',
								'status' => 'pending'
							),
							empty($this->params['named']['post_id']) ? array() : array('post_id' => $this->params['named']['post_id'])
						),
						'params' => array(),
						'active' => (isset($this->params['named']['status']) && ($this->params['named']['status'] == 'pending'))
					),
					'approved' => array(
						'visible' => true,
						'title' => !isset($this->Comment) ? __d('lil_blogs', 'Approved', true) :
						sprintf(__d('lil_blogs', 'Approved (%d)', true), $this->Comment->find('count', array(
							'conditions' => array_merge(
								empty($this->params['named']['post_id']) ? array() : array('Comment.post_id' => $this->params['named']['post_id']),
								array(
									'Post.blog_id' => $blog['Blog']['id'],
									'Comment.status' => LILCOMMENT_APPROVED
								)
							),
							'contain'    => array('Post'),
						))),
						'url'   => array_merge(
							array(
								'plugin' => 'lil_blogs',
								'admin' => true,
								'controller' => 'comments',
								'action' => 'index',
								'status' => 'approved'
							),
							empty($this->params['named']['post_id']) ? array() : array('post_id' => $this->params['named']['post_id'])
						),
						'params' => array(),
						'active' => (isset($this->params['named']['status']) && ($this->params['named']['status'] == 'approved'))
					),
				)
			)
		);
		
		// insert into sidebar right after welcome panel
		$this->sidebarInsertPanel($sidebar, array('after' => 'welcome'), array('blogs' => $blogs));
		
		return $sidebar;
	}
}
?>